package com.cx.mall.demo.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Assistant {
    /**
     * 抽取切点表达式
     */
    @Pointcut(value = "execution(* com.cx.mall.demo.service.impl.UserServiceImpl.*(..))")
    public void myPointcut(){}

    /**
     * 前置通知
     */
    @Before(value = "myPointcut()")
    public static void authentication(){
        System.out.println("身份审核通过");
    }

    /**
     * 后置通知
     * @param jp 连接点参数
     */
    @After(value = "myPointcut()")
    public static void doLog(JoinPoint jp){
        // 获取目标方法
        String type = jp.getSignature().getName();
        System.out.println("记录" + type + "日志");
    }
    @AfterReturning(value = "myPointcut()",returning = "ret")
    public static void doAfterReturning(Object ret){
        System.out.println("返回通知，返回结果为：" + ret);
    }
    @AfterThrowing(value = "myPointcut()",throwing = "e")
    public static void doAfterThrowing(Throwable e){
        System.out.println("异常通知，异常为：" + e);
    }

}
/**
 * 描述: 切面类
 * 实现了目标方法执行时间统计
 * @Aspect
 * @Component
 * public class PerformanceAspect {
 *
 *     @Around("execution(* com.example.service.*.*(..))") // 切入点表达式
 *     public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
 *         long startTime = System.currentTimeMillis();
 *
 *         // 通过JoinPoint获取方法名、参数等信息
 *         String methodName = joinPoint.getSignature().getName();
 *         Object[] args = joinPoint.getArgs();
 *
 *         // 执行目标方法
 *         Object result = joinPoint.proceed();
 *
 *         long endTime = System.currentTimeMillis();
 *         System.out.println("Method " + methodName + " executed in " + (endTime - startTime) + "ms");
 *         return result;
 *     }
 * }
 */